package com.hmkj.admin.controller.commo;

import com.hmkj.admin.controller.BaseController;
import com.hmkj.admin.controller.GenericController;
import com.hmkj.common.entity.ResultEntity;
import com.hmkj.common.entity.dtgrid.DtGrid;
import com.hmkj.core.constant.CacheID;
import com.hmkj.core.exception.BussinessException;
import com.hmkj.core.po.commo.Commo;
import com.hmkj.core.service.cache.RedisService;
import com.hmkj.core.service.commo.CommoService;
import com.xiaoleilu.hutool.util.StrUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;
@Controller
@RequestMapping(BaseController.BASE_URI)
public class CommoJsonController extends GenericController {

    @Resource
    private CommoService commoService;
    @Resource
    private RedisService redisService;

    /**
     * 交易品种列表json数据
     */
    @ResponseBody
    @RequestMapping(value = "commo/json/list", method = RequestMethod.POST)
    @RequiresPermissions("sys:commo:list")
    public DtGrid listJson(String dtGridPager) throws Exception {
        return commoService.getDtGridList(dtGridPager, Commo.class);
    }


    /**
     * 添加
     *
     * @param commo
     * @return
     */
    @ResponseBody
    @RequestMapping("/commo/json/add")
    @RequiresPermissions("sys:commo:add")
    public ResultEntity addJson(Commo commo) throws Exception {
        ResultEntity resultEntity = new ResultEntity();
        //参数校验
        checkModel(commo,"");
        commoService.doAdd(commo);
        return ok();
    }

    /**
     * 修改
     */
    @ResponseBody
    @RequestMapping(value = "/commo/json/edit")
    @RequiresPermissions("sys:commo:edit")
    public ResultEntity edit(Commo commo) throws Exception {
        //参数校验
        checkModel(commo,"update");
        commoService.doEdit(commo);
        return ok();
    }

    /**
     * 修改状态
     */
    @ResponseBody
    @RequestMapping(value = "/commo/json/state")
    @RequiresPermissions("sys:commo:state")
    public ResultEntity state(Integer id, Integer status) throws Exception {
        //参数校验
        if(null == id || null == status){
            throw new BussinessException("参数错误");
        }
        if(status != Commo.STATUS.T1.code && status != Commo.STATUS.T2.code){
            throw new BussinessException("状态参数错误");
        }
        Commo commo = commoService.selectOne(new Commo(c -> c.setId(id)));
        if(null ==commo){
            throw new BussinessException("该交易品种不存在");
        }
        commo.setStatus(status);
        commoService.updateByIdAndVersionSelective(commo);
        redisService.put(CacheID.COMMO_ID_PREFIX + commo.getId(), commo);
        redisService.put(CacheID.COMMO_CODE_PREFIX + commo.getCode(), commo);
        List<Commo> commoList = commoService.select(new Commo(t ->{
            t.setDelFlag(Commo.DELFLAG.NORMAL.code);
            t.setStatus(Commo.STATUS.T1.code);
        }));
        redisService.put(CacheID.COMMO_LIST, commoList);
        return ok();
    }

    /**
     * model校验
     * @return
     */
    void checkModel(Commo model,String type){
        //编辑需要校验Id
        if("update".equals(type)){
            if(null == model.getId()){
                throw new BussinessException("参数有误");
            }
        }
        if(StrUtil.isBlank(model.getName())){
            throw new BussinessException( "请填写品种名称");
        }
        if(StrUtil.isBlank(model.getCode())){
            throw new BussinessException( "请填写品种代码");
        }
        if(null == model.getSortNo()){
            throw new BussinessException( "请填写排序");
        }
    }

}